<?php
class Ingredient{
	private $id;
	private $nom;
	private $prix;
	private $stock;

	public function __construct($id,$nom,$stock,$prix){
		$this->id=$id;
		$this->nom=$nom;
		$this->stock=$stock;
		$this->prix=$prix;
	}
	public static function getAllXML(){
		
	}
	function getXML($isSelected){
		$xml = new DOMDocument;
		$xml->formatOutput = true;
		
		$ingredientTab = $xml->createElement('ingredientTabNode');
		$ingredientTabNode=$xml->appendChild($ingredientTab);
	// Exemple d'utilisation pour l'opérateur ternaire
		$selected = ($isSelected) ? 'ok' : 'nok';
		$ingredient = $xml->createElement('ingredient');
		$ingredientNode = $xml->appendChild($ingredient);
		$ingredientNode->setAttribute('id', $this->id);
		$ingredientNode->appendChild(new DOMElement('nom',$this->nom));
		$ingredientNode->appendChild(new DOMElement('prix',$this->prix));
		$ingredientNode->appendChild(new DOMElement('stock',$this->stock));
		$ingredientNode->appendChild(new DOMElement('selected',$selected));
		
		return $ingredient;
	}
	
	public static function getAll()
	{
		$lstIng = array();
		$dbc = DbConnector::get();
		$query = 'SELECT ingredient_name, ingredient_stock, ingredient_id,ingredient_prix
  					FROM ingredient';
		
		$stmt = $dbc->prepare($query);
		$stmt->execute();
		$res = $stmt->fetchAll(PDO::FETCH_ASSOC);
		
		foreach ($res as $ing)
		{
			$lstIng[] = new Ingredient($ing['ingredient_id'], $ing['ingredient_name'], $ing['ingredient_stock'], $ing['ingredient_stock']);
		}
		return $lstIng;
	}
	public static function compare($ing1,$ing2){
		if($ing1->getID()==$ing2->getID()){
			return 0;
		}
		else{
			return -1;
		}
	}
	public function getID(){
		return $this->id;
	}
	public function getNom(){
		return $this->nom;
	}
	public function getPrix(){
		return $this->prix;
	}
}